/**
 * lilang
 * ProcessHisMangerImpl.java
 * 2013-10-28下午7:23:32
 */
package com.cq.oa.service.work.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oa.base.service.impl.BaseServiceImpl;
import oa.base.util.BeanUtils;
import oa.base.util.Constants;

import com.cq.oa.dao.wfc.ActivityInstanceDao;
import com.cq.oa.dao.wfc.ActivityInstanceHisDao;
import com.cq.oa.dao.wfc.ProcessInstanceDao;
import com.cq.oa.dao.wfc.ProcessInstanceHisDao;
import com.cq.oa.dao.wfc.TaskInstanceDao;
import com.cq.oa.dao.wfc.TaskInstanceHisDao;
import com.cq.oa.domain.wfc.ActivityInstance;
import com.cq.oa.domain.wfc.ActivityInstanceHis;
import com.cq.oa.domain.wfc.ProcessInstance;
import com.cq.oa.domain.wfc.ProcessInstanceHis;
import com.cq.oa.domain.wfc.TaskInstance;
import com.cq.oa.domain.wfc.TaskInstanceHis;
import com.cq.oa.service.work.ProcessHisManger;

/**
 * @author lilang
 *
 */
public class ProcessHisMangerImpl extends BaseServiceImpl<ProcessInstanceHis> implements ProcessHisManger{

	private ProcessInstanceHisDao processInstanceHisDao;
	
	private TaskInstanceHisDao taskInstanceHisDao;
	
	private ActivityInstanceHisDao activityInstanceHisDao;
	
	private ProcessInstanceDao processInstanceDao;
	
	private TaskInstanceDao taskInstanceDao;
	
	
	private ActivityInstanceDao activityInstanceDao;
	
	

	public void setProcessInstanceDao(ProcessInstanceDao processInstanceDao) {
		this.processInstanceDao = processInstanceDao;
	}

	public void setTaskInstanceDao(TaskInstanceDao taskInstanceDao) {
		this.taskInstanceDao = taskInstanceDao;
	}

	public void setActivityInstanceDao(ActivityInstanceDao activityInstanceDao) {
		this.activityInstanceDao = activityInstanceDao;
	}

	public ProcessInstanceHisDao getProcessInstanceHisDao() {
		return processInstanceHisDao;
	}

	public void setProcessInstanceHisDao(ProcessInstanceHisDao processInstanceHisDao) {
		this.processInstanceHisDao = processInstanceHisDao;
	}

	public TaskInstanceHisDao getTaskInstanceHisDao() {
		return taskInstanceHisDao;
	}

	public void setTaskInstanceHisDao(TaskInstanceHisDao taskInstanceHisDao) {
		this.taskInstanceHisDao = taskInstanceHisDao;
	}

	public ActivityInstanceHisDao getActivityInstanceHisDao() {
		return activityInstanceHisDao;
	}

	public void setActivityInstanceHisDao(
			ActivityInstanceHisDao activityInstanceHisDao) {
		this.activityInstanceHisDao = activityInstanceHisDao;
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.work.ProcessHisManger#doBackProcessInstance(com.cq.oa.domain.dto.WorkInstanceDto)
	 */
	public void doBackProcessInstance(String businessId) {
//		Map<String,Object> param = new HashMap<String, Object>();
//		param.put(Constants.BUSINESS_ID, businessId);
//		List<ProcessInstance> instances = this.processInstanceDao.find(param) ;
//		List<TaskInstance> taskInstances = this.taskInstanceDao.find(param);
//		List<ActivityInstance> activityInstances = this.activityInstanceDao.find(param);
//		
//		List<ProcessInstanceHis> instanceHis = new ArrayList<ProcessInstanceHis>();
//		List<TaskInstanceHis> taskHist = new ArrayList<TaskInstanceHis>();
//		List<ActivityInstanceHis> activityInstanceHis = new ArrayList<ActivityInstanceHis>();
//		this.copyListProp(instances, instanceHis);
//		this.copyListActivity(activityInstances, activityInstanceHis);
//		this.copyListTask(taskInstances, taskHist);
//		this.processInstanceHisDao.saveList(instanceHis);
//		this.activityInstanceHisDao.saveList(activityInstanceHis);
//		this.taskInstanceHisDao.saveList(taskHist);
		this.taskInstanceDao.flush();
		this.processInstanceHisDao.doBackHist(businessId);
		this.activityInstanceHisDao.doBackHist(businessId);
		this.taskInstanceHisDao.doBackHist(businessId);
		//清除数据
		this.doClearProcessInstance(businessId);
		
	}
	
	/**
	 * copy 类属性
	 * @param soruces
	 * @param news
	 * @param clazz
	 * @throws Exception
	 * @throws IllegalAccessException
	 * lilang 
	 * 2013-10-30下午5:46:27
	 */
	public void copyListProp(List<ProcessInstance> soruces,List<ProcessInstanceHis> news){
		for(ProcessInstance obj:soruces){
			ProcessInstanceHis target = new ProcessInstanceHis();
			BeanUtils.copyProperties(obj, target);
			news.add(target);
		}
	}
	public void copyListActivity(List<ActivityInstance> sources,List<ActivityInstanceHis> his){
		for(ActivityInstance instance:sources){
			ActivityInstanceHis target = new ActivityInstanceHis();
			BeanUtils.copyProperties(instance, target);
			his.add(target);
		}
	}
	public void copyListTask(List<TaskInstance> soruces,List<TaskInstanceHis> news){
		for(TaskInstance instance:soruces){
			TaskInstanceHis his = new TaskInstanceHis();
			BeanUtils.copyProperties(instance, his);
			news.add(his);
			
		}
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.work.ProcessHisManger#doClearProcessInstance(java.lang.String)
	 */
	public void doClearProcessInstance(String businessId) {
		Map<String,Object> param = new HashMap<String, Object>();
		param.put(Constants.BUSINESS_ID, businessId);
		this.taskInstanceDao.remove(param);
		this.activityInstanceDao.remove(param);
		this.processInstanceDao.remove(param);
	}
	
	
	
	
}
